﻿// 1.1.9.08. Write a method that calculates the sum of two very long positive integer numbers.
// The numbers are represented as array digits and the last digit (the ones) is stored in the array at index 0.
// Make the method work for all numbers with length up to 10,000 digits.

using System;

class CustomBigInteger
{
    public static string ReverseNumber(string num)
    {
        char[] nummy = new char[num.ToString().Length];
        nummy = num.ToString().ToCharArray();
        for (int i = 0; i < num.ToString().Length / 2; i++)
        {
            char firstTemp = nummy[i];
            nummy[i] = nummy[num.ToString().Length - 1 - i];
            nummy[num.ToString().Length - 1 - i] = firstTemp;
        }
        num = string.Join("", nummy);
        return num;
    }

    public static string SumOfCustomBigInteger(string first, string second)
    {
        byte[] firsty = new byte[first.ToString().Length];
        byte[] secondy = new byte[second.ToString().Length];
        for (int i = 0; i < first.Length; i++)
        {
            firsty[i] = (byte)(first[i] - 48);
        }
        for (int i = 0; i < second.Length; i++)
        {
            secondy[i] = (byte)(second[i] - 48);
        }
        int longestLen = firsty.Length;
        int shortestLen = secondy.Length;
        string longest = "first";
        if (secondy.Length > firsty.Length)
        {
            longestLen = secondy.Length;
            shortestLen = firsty.Length;
            longest = "second";
        }
        byte[] result = new byte[longestLen + 1];

        for (int i = 0; i < longestLen; i++)
        {
            if (i < shortestLen)
            {
             
                result[i] += (byte)(firsty[i] + secondy[i]);
            }
            else
            {
                if (longest == "first")
                {
                    result[i] += firsty[i];
                }
                else
                {
                    result[i] += secondy[i];
                }
            }
            
            if (result[i] > 9)
            {
                result[i + 1]++;
                result[i] -= 10;
            }
        }
        string resulty = string.Join("", result);
        return resulty;
    }

    static void Main()
    {
        string first = "315915698";
        string reversedFirst = ReverseNumber(first);

        string second = "637641534490067";
        string reversedSecond = ReverseNumber(second);

        string result = ReverseNumber(SumOfCustomBigInteger(reversedFirst, reversedSecond));

        Console.WriteLine("{0:0000000000000000}", result);
        Console.WriteLine("{0:0000000000000000}", 315915698 + 637641534490067);
    }
}